﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NUnit.Framework;
using Smith.AMC;

namespace MemcachedClientTests
{
    [TestFixture]
    public class IncrementTest
    {

        [Test]
        public void IncrementShouldIncrementValue()
        {
            MemcachedClient client = new MemcachedClient();
            AutoResetEvent evt = new AutoResetEvent(false);
            client.BeginSetValueForIncrement(
                "test", 
                10, 
                ar =>
            {
                AutoResetEvent evtInner = (AutoResetEvent)ar.AsyncState;
                try
                {
                    bool success = client.EndSetValueForIncrement(ar);
                    Assert.IsTrue(success);
                    
                }
                finally
                {
                    evtInner.Set();
                }
            }, evt);
            evt.WaitOne();

            client.BeginIncrement(
               "test",
               1,
               ar =>
               {
                   AutoResetEvent evtInner = (AutoResetEvent)ar.AsyncState;
                   try
                   {
                       ulong newValue = client.EndIncrement(ar);
                       Console.WriteLine(newValue);
                       Assert.That(newValue, Is.EqualTo(11));

                   }
                   finally
                   {
                       evtInner.Set();
                   }
               }, evt);
            evt.WaitOne();

        }


        [Test]
        public void DecrementShouldDecrementValue()
        {
            MemcachedClient client = new MemcachedClient();
            AutoResetEvent evt = new AutoResetEvent(false);
            client.BeginSetValueForIncrement(
                "test2",
                10,
                ar =>
                {
                    AutoResetEvent evtInner = (AutoResetEvent)ar.AsyncState;
                    try
                    {
                        bool success = client.EndSetValueForIncrement(ar);
                        Assert.IsTrue(success);

                    }
                    finally
                    {
                        evtInner.Set();
                    }
                }, evt);
            evt.WaitOne();

            client.BeginDecrement(
               "test2",
               1,
               ar =>
               {
                   AutoResetEvent evtInner = (AutoResetEvent)ar.AsyncState;
                   try
                   {
                       ulong newValue = client.EndDecrement(ar);
                       Console.WriteLine(newValue);
                       Assert.That(newValue, Is.EqualTo(9));

                   }
                   finally
                   {
                       evtInner.Set();
                   }
               }, evt);
            evt.WaitOne();
        }
    }
}
